package TAIC.Google ;
import java.io.*;
import java.util.* ;
import java.net.* ;
// Search web:  http://www.google.com/search?hl=en&q=pencil+box&num=1
// swrnum=2450000"

class GWeb {
    StringBuffer str = null ;  
    String line;
	Socket s  = null;
	
    static public void main ( String agu [] ){
		System.out.println  ((new GWeb()).getWebCount( "pencil box" ) );
    }
	
	public Long getWebCount ( String query ) {
		int i ;
		String q ; 
		q = query.trim().replace ( " " , "+" ); 
        BufferedReader buffer = null;
        str = new StringBuffer ();
        str.ensureCapacity( 20000 ) ;      
		
		for ( i = 0 ; i < 10 ; i ++ )
		try {
			s = new Socket("www.google.com",80);        
            s.setSoTimeout( 10000 ) ;
        	PrintStream p = new PrintStream( s.getOutputStream() );
        	p.print( "GET /search?hl=en&q=" + q + "&num=1\n" );
            InputStreamReader in = new InputStreamReader(s.getInputStream());
			buffer = new BufferedReader(in);
            while ((line = buffer.readLine()) != null) str.append ( line ) ;
            in.close();
			s.close ();
			break ; 
		}
		catch ( Exception e ) {
			System.out.println ( "Google Image Search for query \"" + query + "\" fails for the " + ( i+ 1) + "th time." );
		}
		if ( i >= 10 ) {
			System.out.println ( "Google Image Search Failed for query \"" + query + "\"" ) ;
			return (long) 100000  ; 
		} else { 
			int index1 , index2 ; 
			index1 = str.indexOf ( "swrnum=" ) ;
			index2 = str.indexOf ( "\"" , index1 ) ; 
			if ( index1 == -1 ) return ( long ) 100000 ; 
			return Long.valueOf ( str.substring ( index1 + 7 , index2 )) ;
		}
	}

}